package createArff;

import Types.DrugDDIFile;
import Types.Sentence;
import java.util.*;

public class ListOfWords {

    List<String> lstWords;
    List<String> lstBiWords;
    HashMap<String, Integer> hmWords;
    HashMap<String, Integer> hmBiWords;

    public ListOfWords() {
        lstWords = new ArrayList<String>();
        lstBiWords = new ArrayList<String>();
        hmWords = new HashMap<String, Integer>();
        hmBiWords = new HashMap<String, Integer>();
    }

    public List<String> getLstWords() {
        return lstWords;
    }

    public List<String> getLstBiWords() {
        return lstBiWords;
    }

    public HashMap<String, Integer> getHmWords() {
        return hmWords;
    }

    public HashMap<String, Integer> getHmBiWords() {
        return hmBiWords;
    }

    public void createListOfWords(List<DrugDDIFile> files) {
        for (int i = 0; i < files.size(); i++) {
            createListOfWords(files.get(i));
        }
        Iterator it = hmWords.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry ent = (Map.Entry) it.next();
            if ((Integer) ent.getValue() >= 2) {
                lstWords.add((String) ent.getKey());
            }
        }
        it = hmBiWords.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry ent = (Map.Entry) it.next();
            if ((Integer) ent.getValue() >= 3) {
                lstBiWords.add((String) ent.getKey());
            }
        }
    }

    public void createListOfWords(DrugDDIFile file) {
        List<Sentence> lstSentence = file.getLstSentence();
        for (int i = 0; i < lstSentence.size(); i++) {
            if (lstSentence.get(i).getDdiList().size() > 0) {
                Sentence sentence = lstSentence.get(i);
                String text = sentence.getSimplifiedText();
                String[] terms;
                terms = text.split(" ");//all words
                addTermsToLists(terms);
//                terms = returnWordsBeforeFirstDrug(text);
//                addTermsToLists(terms);
//                terms = returnWordsBetweenDrugs(text);
//                addTermsToLists(terms);
//                terms = returnWordsAfterSecondDrugs(text);
//                addTermsToLists(terms);
            }
        }
    }

    void addTermsToLists(String[] terms) {
        for (int j = 0; j < terms.length; j++) {
            String term = terms[j];
            if (!term.isEmpty() && term.length() > 1) {
                if (hmWords.containsKey(term)) {
                    hmWords.put(term, hmWords.get(term) + 1);
                } else {
                    hmWords.put(term, 1);
                }
            }
        }
        for (int j = 0; j < terms.length - 1; j++) {
            if (terms[j].length() > 1 && terms[j+1].length() > 1) {
                String bi = terms[j] + "_" + terms[j + 1];
                if (hmBiWords.containsKey(bi)) {
                    hmBiWords.put(bi, hmBiWords.get(bi) + 1);
                } else {
                    hmBiWords.put(bi, 1);
                }
            }
        }
    }

    public String[] returnWordsBeforeFirstDrug(String text) {
        List<String> lstSomeWords = new ArrayList<String>();
        int firstDrug = text.indexOf("replaceddrug");
        if (firstDrug > -1) {
            String firstPart = text.substring(0, firstDrug).trim();
            String[] temAr = firstPart.split(" ");
            for (int p = temAr.length - 1; p > temAr.length - 4 && p >= 0; p--) {
                if (temAr[p].length() > 1) {
                    lstSomeWords.add(temAr[p]);
                }
            }
        }
        return lstSomeWords.toArray(new String[lstSomeWords.size()]);
    }

    public String[] returnWordsBetweenDrugs(String text) {
        List<String> lstSomeWords = new ArrayList<String>();
        int firstDrug = text.indexOf("replaceddrug");
        if (firstDrug > -1) {
            int secondDrug = text.indexOf("replaceddrug", firstDrug + 12);
            if (secondDrug > -1) {
                String betPart = text.substring(firstDrug + 12, secondDrug).trim();
                String[] temAr = betPart.split(" ");
                for (int p = 0; p < temAr.length; p++) {
                    if (temAr[p].length() > 1) {
                        lstSomeWords.add(temAr[p]);
                    }
                }
            }
        }
        return lstSomeWords.toArray(new String[lstSomeWords.size()]);
    }

    public String[] returnWordsAfterSecondDrugs(String text) {
        List<String> lstSomeWords = new ArrayList<String>();
        int firstDrug = text.indexOf("replaceddrug");
        if (firstDrug > -1) {
            int secondDrug = text.indexOf("replaceddrug", firstDrug + 12);
            if (secondDrug > -1) {
                String lastPart = text.substring(secondDrug + 12).trim();
                String[] temAr = lastPart.split(" ");
                for (int p = 0; p < 3 && p < temAr.length; p++) {
                    if (temAr[p].length() > 1) {
                        lstSomeWords.add(temAr[p]);
                    }
                }
            }
        }
        return lstSomeWords.toArray(new String[lstSomeWords.size()]);
    }
}
